﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinalTestMachine.Motion.Product
{
    /// <summary>
    /// 产品信息
    /// </summary>
    public class ProductInfo
    {
        public ProductInfo()
        {
            Result = ProductInfo.ResultOK;
            InitTestStation();
        }
        private List<ProductTestStation> productTestStations = new List<ProductTestStation>();
        private void InitTestStation()
        {
            for (int i = 0; i < GlobalParams.WorkstationSum; i++)
            {
                productTestStations.Add(new ProductTestStation());
            }
        }
        public ProductTestStation GetTestStation(int index)
        {
            if(index <= 0 || index > GlobalParams.WorkstationSum)
            {
                throw new ArgumentOutOfRangeException($"获取测试站数据失败，请确保站位索引在({1},{GlobalParams.WorkstationSum})之间，当前索引为{index}");
            }
            return productTestStations[index-1];
        }
        /// <summary>
        /// 测试结果OK
        /// </summary>
        public const string ResultOK = "OK";

        /// <summary>
        /// 测试结果NG
        /// </summary>
        public const string ResultNG = "NG";

        /// <summary>
        /// 产品条码
        /// </summary>
        public string Number { get; set; }

        /// <summary>
        /// 当前产品测试结果
        /// </summary>
        public string Result { get; set; }

        /// <summary>
        /// 失败的原因
        /// </summary>
        public string FailedMsg { get; set; }

        /// <summary>
        /// A/B/C/D
        /// </summary>
        public string PARA { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public string StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public string EndTime { get; set; }

        /// <summary>
        /// 治具编码
        /// </summary>
        public string Socket { get; set; }

        /// <summary>
        /// 上传MES的信息
        /// </summary>
        public string DataUpInfo { get; set; }

        private string _messtartTime = "";
        private string _mesendTime = "";

        #region MES

        /// <summary>
        /// 时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        protected static string GetTimeStamp(DateTime dt)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
            long timeStamp = (long)(dt - startTime).TotalMilliseconds;
            return timeStamp.ToString();
        }

        public static string GetMesCurrentTime()
        {
            DateTime dtNow = DateTime.Now;
            string ts = GetTimeStamp(dtNow);
            return ts;
        }

        public void SetCurrentTimeToMesStartTime()
        {
            MesStartTime = GetMesCurrentTime();
        }

        public void SetCurrentTimeToMesEndTime()
        {
            MesEndTime = GetMesCurrentTime();
        }

        public string MesStartTime
        {
            get { return _messtartTime; }
            set { _messtartTime = value; }
        }

        public string MesEndTime
        {
            get { return _mesendTime; }
            set { _mesendTime = value; }
        }

        #endregion MES

        

        public IEnumerable<ProductTestStation> GetProductTestStationCollection()
        {
            foreach (var item in productTestStations)
            {
                yield return item;
            }
        }

        public void Clear()
        {
            Number = "";
            Result = "";
            FailedMsg = "";
        }

        public override string ToString()
        {
            return $"Number:{Number},Result:{Result}";
        }

        public List<ProductTestStationTestItem> GetAllTestItems()
        {
            List<ProductTestStationTestItem> items = new List<ProductTestStationTestItem>();
            //遍历每一个工站
            foreach (var item in productTestStations)
            {
                //遍历每一个工站的测试项
                foreach (var testItem in item.StationTestItems)
                {
                    items.Add(testItem);
                }
            }
            return items;
        }
    }
}